home *** CD-ROM | disk | FTP | other *** search
/ MacHack 2001 / MacHack 2001.toast / pc / The Hacks / Pro Mousing / Pro Mousing.c < prev    next >
Encoding:
C/C++ Source or Header  |  2001-06-23  |  6.1 KB  |  248 lines

  1. #include <Resources.h>
  2. #include <Icons.h>
  3. #include <OSUtils.h>
  4. #include <Sound.h>
  5. #include <Quickdraw.h>
  6. #include <QDOffscreen.h>
  7. #include <ControlDefinitions.h>
  8.  
  9. enum
  10. {
  11.     uppGetNextEventProcInfo = kPascalStackBased
  12.         | RESULT_SIZE( SIZE_CODE( sizeof( Boolean ) ) )
  13.         | STACK_ROUTINE_PARAMETER(1, SIZE_CODE(sizeof(short)))
  14.         | STACK_ROUTINE_PARAMETER(2, SIZE_CODE(sizeof(EventRecord *)))
  15. };
  16.  
  17. enum
  18. {
  19.     uppFrontWindowProcInfo = kPascalStackBased
  20.         | RESULT_SIZE( SIZE_CODE( sizeof( WindowPtr ) ) )
  21. };
  22.  
  23. enum
  24. {
  25.     uppButtonProcInfo = kPascalStackBased
  26.         | RESULT_SIZE( SIZE_CODE( sizeof( Boolean ) ) )
  27. };
  28.  
  29. #define    kPauseLength    30
  30. #define kMovementWindow    2
  31.  
  32. pascal Boolean NewGetNextEvent( short eventMask, EventRecord *theEvent );
  33. pascal WindowPtr NewFrontWindow( void );
  34. pascal Boolean NewButton( void );
  35. void ChangeWindow( WindowPtr theWindow, Boolean makeVisible );
  36. void MeasurePause( short coefficient );
  37.  
  38. UniversalProcPtr    gOldGetNextEvent = nil, gNewGetNextEvent;
  39. UniversalProcPtr    gOldFrontWindow = nil, gNewFrontWindow;
  40. UniversalProcPtr    gOldButton = nil, gNewButton;
  41. WindowPtr            myFrontWindow = nil;
  42. Boolean                selectWindow = true, lockButton = false, redrawWindow = false;
  43. Point                mouseLoc;
  44. short                theRatio = 1;
  45. long                ticks = 0;
  46.  
  47. void main(void)
  48. {
  49.     OSErr    err = noErr;
  50.     THz        oldZone;
  51.     
  52.     oldZone = GetZone();
  53.     SetZone( SystemZone() );
  54.     
  55.     DetachResource(GetResource('INIT', 0));
  56.     
  57.     gOldGetNextEvent = GetToolTrapAddress( _GetNextEvent );
  58.     gNewGetNextEvent = NewRoutineDescriptor( (ProcPtr)&NewGetNextEvent, uppGetNextEventProcInfo,kPowerPCISA);
  59.     SetToolTrapAddress( gNewGetNextEvent, _GetNextEvent );
  60.     
  61.     gOldFrontWindow = GetToolTrapAddress( _FrontWindow );
  62.     gNewFrontWindow = NewRoutineDescriptor( (ProcPtr)&NewFrontWindow, uppFrontWindowProcInfo,kPowerPCISA);
  63.     SetToolTrapAddress( gNewFrontWindow, _FrontWindow );
  64.     
  65.     gOldButton = GetToolTrapAddress( _Button );
  66.     gNewButton = NewRoutineDescriptor( (ProcPtr)&NewButton, uppButtonProcInfo, kPowerPCISA);
  67.     SetToolTrapAddress( gNewButton, _Button );
  68.     
  69.     SetZone(oldZone);
  70. }
  71.  
  72. pascal Boolean NewGetNextEvent( short eventMask, EventRecord *theEvent )
  73. {
  74.     Boolean            result;
  75.     WindowPtr        theWindow, realFrontWindow, cursorWindow;
  76.     Point            thePoint = theEvent->where;
  77.     ControlHandle    theControl;
  78.     short            thePart, controlPart;
  79.     
  80.     result = CallUniversalProc( gOldGetNextEvent, uppGetNextEventProcInfo, eventMask, theEvent );
  81.     
  82.     thePart = FindWindow( theEvent->where, &theWindow );
  83.     cursorWindow = theWindow;
  84.     
  85.     realFrontWindow = (WindowPtr)CallUniversalProc( gOldFrontWindow, uppFrontWindowProcInfo );
  86.     
  87.     if ( theEvent->what == mouseDown )
  88.     {
  89.         if ( thePart == inContent || thePart == inDrag )
  90.         {
  91.             if ( GetWVariant( realFrontWindow ) != movableDBoxProc && GetWVariant( realFrontWindow ) != dBoxProc && GetWVariant( realFrontWindow ) != plainDBox )
  92.                 if ( theWindow != (WindowPtr)CallUniversalProc( gOldFrontWindow, uppFrontWindowProcInfo ) )
  93.                     if ( ( theEvent->modifiers & cmdKey ) == false )
  94.                         SelectWindow( theWindow );
  95.         }
  96.     }
  97.     
  98.     if ( GetWVariant( realFrontWindow ) == movableDBoxProc || GetWVariant( realFrontWindow ) == dBoxProc )
  99.         return result;
  100.     else if ( theWindow == nil && myFrontWindow != nil )
  101.     {
  102.         if ( myFrontWindow != nil )
  103.             ChangeWindow( myFrontWindow, false );
  104.         
  105.         theWindow = (WindowPtr)CallUniversalProc( gOldFrontWindow, uppFrontWindowProcInfo );
  106.         
  107.         ChangeWindow( theWindow, true );
  108.         myFrontWindow = nil;
  109.     }
  110.     else if ( theWindow != myFrontWindow )
  111.     {
  112.         if ( myFrontWindow != nil )
  113.             ChangeWindow( myFrontWindow, false );
  114.         
  115.         ChangeWindow( theWindow, true );
  116.         myFrontWindow = theWindow;
  117.         
  118.         theWindow = (WindowPtr)CallUniversalProc( gOldFrontWindow, uppFrontWindowProcInfo );
  119.         if ( theWindow != myFrontWindow )
  120.             ChangeWindow( theWindow, false );
  121.     }
  122.     
  123.     SetPort( cursorWindow );
  124.     
  125.     GlobalToLocal( &thePoint );
  126.     theControl = FindControlUnderMouse( thePoint, cursorWindow, &controlPart );
  127.     
  128.     if ( thePart == inDrag || thePart == inGrow || thePart == inMenuBar )
  129.         MeasurePause( 1 );
  130.     else if ( theControl && controlPart == kControlIndicatorPart )
  131.         MeasurePause( 1 );
  132.     else if ( theControl && controlPart == kControlButtonPart )
  133.     {
  134.         if ( ticks == 0 )
  135.             ticks = TickCount();
  136.         
  137.         if ( TickCount() - ticks >= kPauseLength * 2 )
  138.         {
  139.             PostEvent( mouseDown, 0 );
  140.             ticks = 0;
  141.         }
  142.         
  143.         theRatio = 2;
  144.     }
  145.     else if ( thePart == inGoAway )
  146.     {
  147.         if ( ticks == 0 )
  148.             ticks = TickCount();
  149.         
  150.         if ( TickCount() - ticks >= kPauseLength * 4 )
  151.         {
  152.             PostEvent( mouseDown, 0 );
  153.             ticks = 0;
  154.         }
  155.         
  156.         theRatio = 4;
  157.     }
  158.     else if ( thePart == inCollapseBox || thePart == inZoomIn || thePart == inZoomOut )
  159.     {
  160.         if ( ticks == 0 )
  161.             ticks = TickCount();
  162.         
  163.         if ( TickCount() - ticks >= kPauseLength * 2 )
  164.         {
  165.             PostEvent( mouseDown, 0 );
  166.             ticks = 0;
  167.         }
  168.         
  169.         theRatio = 2;
  170.     }
  171.     else if ( ticks != 0 )
  172.         ticks = 0;
  173.     
  174.     return result;
  175. }
  176.  
  177. pascal WindowPtr NewFrontWindow( void )
  178. {
  179.     WindowPtr    theWindow;
  180.     
  181.     theWindow = (WindowPtr)CallUniversalProc( gOldFrontWindow, uppFrontWindowProcInfo );
  182.     
  183.     if ( GetWVariant( theWindow ) == movableDBoxProc || GetWVariant( theWindow ) == dBoxProc )
  184.         return theWindow;
  185.     
  186.     if ( theWindow != myFrontWindow )
  187.         return myFrontWindow;
  188.     else
  189.         return theWindow;
  190. }
  191.  
  192. pascal Boolean NewButton( void )
  193. {
  194.     Boolean        button;
  195.     
  196.     if ( lockButton == true )
  197.     {
  198.         Point    curMouse;
  199.         
  200.         if ( ticks == 0 )
  201.             ticks = TickCount();
  202.         else if ( TickCount() - ticks >= kPauseLength * theRatio )
  203.         {
  204.             lockButton = false;
  205.             ticks = 0;
  206.             
  207.             return true;
  208.         }
  209.         
  210.         GetMouse( &curMouse );
  211.         
  212.         if ( ( curMouse.h - mouseLoc.h ) > kMovementWindow || ( curMouse.h - mouseLoc.h ) < -kMovementWindow || ( curMouse.v - mouseLoc.v ) > kMovementWindow || ( curMouse.v - mouseLoc.v ) < -kMovementWindow )
  213.         {
  214.             ticks = TickCount();
  215.             GetMouse( &mouseLoc );
  216.         }
  217.         
  218.         return true;
  219.     }
  220.     
  221.     button = (Boolean)CallUniversalProc( gOldButton, uppButtonProcInfo );
  222.     
  223.     return button;
  224. }
  225.  
  226. void ChangeWindow( WindowPtr theWindow, Boolean makeVisible )
  227. {
  228.     if ( selectWindow && makeVisible )
  229.         SelectWindow( theWindow );
  230.     else
  231.         HiliteWindow( theWindow, makeVisible );
  232. }
  233.  
  234. void MeasurePause( short coefficient )
  235. {
  236.     if ( ticks == 0 )
  237.         ticks = TickCount();
  238.     
  239.     if ( TickCount() - ticks >= kPauseLength * coefficient )
  240.     {
  241.         PostEvent( mouseDown, 0 );
  242.         lockButton = true;
  243.         GetMouse( &mouseLoc );
  244.         ticks = 0;
  245.     }
  246.     
  247.     theRatio = coefficient;
  248. }